<?php
declare(strict_types=1);

namespace App\Extionsion;

use App\Controller;
use Field\StoreProductConst;

// hook controller_extionsion_index_use.php

/**
 * @title   扩展
 * @auth    true
 * @login   false
 * @menu    false
 * @component vlist/index.vue
 * @icon el-icon-menu
 * @rank    10
 */
class Index extends Controller
{
    // hook controller_extionsion_index_public_start.php

    // hook controller_extionsion_index_public_end.php

    /**
     * @title    首页
     * @rank    99
     */
    public function action_Index()
    {
        // hook controller_extionsion_index_index_start.php
        //获取参数
        $isShowPricy = $this->request->param('privacy',0);        
        
        return $this->View(get_defined_vars());
        // hook controller_extionsion_index_index_end.php
    }


     /**
     * @title    exe安装
     * @rank    99
     */
    public function action_Exe()
    {
        // hook controller_extionsion_index_exe_start.php
        
        
        return $this->View(get_defined_vars());
        // hook controller_extionsion_index_exe_end.php
    }
    

    /**
     * @title    意见
     * @rank    99
     */
    public function action_suggest()
    {
        // hook controller_extionsion_index_index_start.php
        //获取参数
        $shopName = $this->request->param('shopname','');        
        
        return $this->View(get_defined_vars());
        // hook controller_extionsion_index_index_end.php
    }

    /**
     * @title    提交反馈
     * @rank    99
     */
    public function action_submit_feedback()
    {
        // 获取POST数据
        $account = $this->request->param('account', '');
        $action = $this->request->param('action', '');
        $content = $this->request->param('content', '');
        $timestamp = $this->request->param('timestamp', '');
        $userAgent = $this->request->param('userAgent', '');
        $url = $this->request->param('url', '');        
        // 验证必填字段
        if (empty($account) || empty($action) || empty($content)) {
            return $this->response('1001', [], '必填字段不能为空');
        }
        
        // 准备提交数据
        $submitData = [
            'account' => $account,
            'action' => $action,
            'content' => $content,
            'timestamp' => $timestamp,
            'userAgent' => $userAgent,
            'ip'=>$this->request->get_client_ip(),
            'url' => $url
        ];
        
        // 发送到外部API
        $apiUrl = 'https://ext.lihuibox.com/api/logs';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $apiUrl);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($submitData));
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Content-Type: application/json',
            'Accept: application/json'
        ]);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $error = curl_error($ch);
        curl_close($ch);
        
        if ($error) {
            return $this->response('1002', [], '网络错误：' . $error);
        }
        
        if ($httpCode !== 200) {
            return $this->response('1003', [], 'API请求失败，HTTP状态码：' . $httpCode);
        }
        
        return  $response;
        
    }





    // hook controller_extionsion_index_end.php
}

?>